package com.team12.hotel404.controller.admin;

import com.team12.hotel404.domain.Role;
import com.team12.hotel404.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
@RequestMapping(value = "/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/list")
    public String list_all_role(Model model){
        List<Role> roles = roleService.listAllRole();
        model.addAttribute("roles",roles);
        return "admin/role-list";
    }

    @RequestMapping(value = "/insert",method = RequestMethod.GET)
    public String insert_role(){
        return "admin/role-insert";
    }
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public String insert_role(Role role){
        roleService.insertRole(role);
        return "redirect:/role/list";
    }
    @RequestMapping(value = "/del",method = RequestMethod.POST)
    public String del_role(Integer id){
        roleService.deleteRole(id);
        return "redirect:/role/list";
    }

    @RequestMapping(value = "findRoleById")
    public String find_role_by_id(Integer id){
        roleService.findRoleById(id);
        return "admin/role-update";
    }
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update_role(Role role){
        roleService.updateRoleInfo(role);
        return "redirect:/role/list";
    }
}
